package com.hk.admin.module.system.service;

import com.hk.admin.common.constant.GlobalEnum;
import com.hk.admin.common.base.dto.message.Message;
import com.hk.admin.common.base.dto.page.Pagination;
import com.hk.admin.common.dto.system.role.RoleDTO;
import com.hk.admin.common.dto.system.role.RoleSearchDTO;
import com.hk.admin.common.model.system.MenuModel;
import com.hk.admin.common.model.system.RoleModel;
import com.hk.admin.common.model.system.UserModel;
import com.hk.admin.common.base.service.BaseService;
import com.hk.admin.module.system.dao.MenuRepository;
import com.hk.admin.module.system.dao.RoleRepository;
import com.hk.admin.module.system.dao.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleService extends BaseService {

    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private MenuRepository menuRepository;
    @Autowired
    private UserRepository userRepository;

    /**
     * 查询所有
     */
    public ResponseEntity<Message> list(RoleSearchDTO dto) {
        // 分页查询
        Page<RoleModel> pageList = roleRepository.findAll(RoleModel.createSpecification(dto), getPageable(dto));
        // 处理数据返回到前端
        Pagination pagination = transformModelIntoDTO(pageList);
        return success(pagination);
    }

    /**
     * 处理返回前端数据
     */
    private Pagination transformModelIntoDTO(Page<RoleModel> pageList) {
        Pagination pagination = new Pagination(pageList);
        List<RoleModel> list = pageList.getContent();
        List<RoleDTO> dtoList = new ArrayList<>();
        list.forEach(model -> dtoList.add(new RoleDTO(model)));
        pagination.setContent(dtoList);
        return pagination;
    }

    /**
     * 查询一个
     */
    public ResponseEntity<Message> query(Long id) {
        RoleModel model = roleRepository.findOne(id);
        if (model == null) {
            return idError();
        }
        RoleDTO dto = new RoleDTO(model);
        dto.fitMenuIds(model);
        return success(dto);
    }

    /**
     * 保存
     */
    public ResponseEntity<Message> save(RoleDTO dto) {
        addOrUpdate(dto);
        return saveSuccess();
    }

    /**
     * 添加或者更新
     */
    private boolean addOrUpdate(RoleDTO dto) {
        RoleModel model = null;
        if (dto.getId() != null) {
            model = roleRepository.findOne(dto.getId());
        }
        if (model == null) {
            model = new RoleModel(dto);
        } else {
            model.update(dto);
        }
        // 更新角色和菜单的关系
        List<MenuModel> menus = menuRepository.findByIdIn(dto.getMenuIds());
        model.setMenus(menus);
        roleRepository.save(model);
        return true;
    }

    /**
     * 删除
     */
    public ResponseEntity<Message> delete(Long id) {
        RoleModel model = roleRepository.findOne(id);
        if (model == null) {
            return idError();
        }
        model.delete();
        roleRepository.save(model);
        return saveSuccess();
    }


    /**
     * 当前登陆人的角色
     */
    public ResponseEntity<Message> getRoleList(String phoneNumber) {
        UserModel model = userRepository.findByPhoneNumberAndDeleted(phoneNumber, GlobalEnum.DELETED.NO.getKey());
        List<RoleModel> roleList;
        // 如果是超级管理员
        if (model.iAmASuperAdmin()) {
            roleList = roleRepository.findByDeleted(GlobalEnum.DELETED.NO.getKey());
        } else {
            roleList = model.getRoles();
        }
        // 返回DTO
        List<RoleDTO> roleDTOList = roleModelToDTO(roleList);
        return success(roleDTOList);
    }

    /**
     * 返回DTO对象
     */
    private List<RoleDTO> roleModelToDTO(List<RoleModel> roleList) {
        List<RoleDTO> dtoList = new ArrayList<>();
        roleList.forEach(model -> dtoList.add(new RoleDTO(model)));
        return dtoList;
    }

}
